var http = require('http');
var fs = require('fs');
var sys = require('sys');
var	f = "test.txt",
    startByte = 0;

http.createServer(function(req, res){
    /*
    fs.readFile('test.txt',function (err, data){
        res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
        res.write(data);
        res.end();
    });*/

    fs.stat(f, function(err, stats){
	  if (err) throw err;
	  startByte = stats.size;
	  sys.puts(startByte);
    });

	fs.watchFile(f, function (curr, prev) {
	  fs.stat(f, function(err, stats){
	    if (err) throw err;
	    fs.createReadStream(f, {
	      start: startByte,
	      end: stats.size
	    }).addListener("data", function(lines) {
	      //sys.puts(lines);
  		res.writeHead(200, {'Content-Type': 'text/html'});
		res.write(lines);
		res.end();
	      startByte = stats.size;
	    });
	  });
	});
}).listen(8000);


